package com.canbot.u05.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.canbot.u05.bean.AudioInfo;

import java.util.List;

public class AudioDomain {

        protected static final int MEIDIA_STORE_AUDIO = 1;

        private AudioController mAudioController;

        public AudioDomain(Context context) {
                mAudioController = new AudioController(context);
        }

        /**
         * 获得音频
         *
         * @param listener 返回集合
         */
        public void getAudio(final OnAudioListener listener) {
                final Handler handler = new Handler() {
                        @SuppressWarnings("unchecked")
                        public void handleMessage(android.os.Message msg) {
                                switch (msg.what) {
                                        case MEIDIA_STORE_AUDIO:
                                                List<AudioInfo> medias = (List<AudioInfo>) msg.obj;
                                                if (medias != null) {
                                                        Log.e("info", "List = " + medias.size());
                                                        listener.onResult(medias);
                                                }
                                                break;
                                }
                        }
                };
                new Thread() {

                        public void run() {
                                //获取媒体库的音频文件
                                List<AudioInfo> media = mAudioController.getMedia();
                                Message msg = handler.obtainMessage();
                                msg.what = MEIDIA_STORE_AUDIO;
                                msg.obj = media;
                                msg.sendToTarget();
                        }

                        ;
                }.start();
        }
}
